package com.cong.modules.meeting.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cong.common.util.PageUtil;
import com.cong.common.util.StringUtils;
import com.cong.common.util.UUIDUtil;
import com.cong.common.vo.AjaxResultVo;
import com.cong.modules.base.BaseController;
import com.cong.modules.meeting.model.MeetingRecords;
import com.cong.modules.meeting.service.IMeetingRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 会议纪要 前端控制器
 */
@Controller
@RequestMapping("/meetingRecords")
@Tag(name = "MeetingRecordsController", description = "会议纪要管理")
public class MeetingRecordsController extends BaseController {

    @Resource
    private IMeetingRecordsService meetingRecordsService;

    /**
     * 会议纪要管理--会议纪要列表
     */
    @GetMapping("/toMeetingRecordsPage")
    @Operation(summary = "跳转到会议纪要列表页面", description = "跳转到会议纪要列表页面")
    public String meetingRecordsList() {
        return "meetingrecords/list";
    }

    /**
     * 会议纪要列表数据
     *
     * @param meetingRecords 会议纪要实体
     * @param limit 查询限制条数
     * @param offset 查询游标
     * @return AjaxResultVo 分页列表数据
     */
    @RequiresPermissions("meetingRecords:list")
    @PostMapping("/list")
    @ResponseBody
    @Operation(summary = "获取会议纪要列表数据", description = "根据查询条件获取会议纪要的分页列表数据")
    @Parameter(name = "meetingRecords", description = "会议纪要实体", required = false)
    @Parameter(name = "limit", description = "每页显示条数", required = true)
    @Parameter(name = "offset", description = "当前页码", required = true)
    public AjaxResultVo pageMeetingRecords(MeetingRecords meetingRecords, Integer limit, Integer offset) {
        try {
            PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
            List<MeetingRecords> recordsList = meetingRecordsService.listMeetingRecords(meetingRecords);
            PageInfo<MeetingRecords> pageResult = new PageInfo<>(recordsList);
            AjaxResultVo resultVo = AjaxResultVo.success();
            resultVo.put("rows", pageResult.getList());
            resultVo.put("total", pageResult.getTotal());
            return resultVo;
        } catch (Exception e) {
            logger.error(String.format("会议纪要列表查询失败 MeetingRecordsController.pageMeetingRecords%s", e));
            return AjaxResultVo.error("查询失败");
        }
    }

    /**
     * 跳转新增会议纪要
     *
     * @param model 页面跳转实体
     * @return 新增会议纪要页面
     */
    @RequiresPermissions("meetingRecords:add")
    @GetMapping("/add")
    @Operation(summary = "跳转到新增会议纪要页面")
    public String add(Model model) {
        return "meetingrecords/add";
    }

    /**
     * 添加会议纪要数据
     *
     * @param meetingRecords 插入会议纪要参数
     * @return AjaxResultVo 添加会议纪要结果
     */
    @RequiresPermissions("meetingRecords:add")
    @PostMapping("/doAdd")
    @ResponseBody
    @Operation(summary = "添加会议纪要", description = "插入新的会议纪要记录")
    @Parameter(name = "meetingRecords", description = "会议纪要实体", required = true)
    public AjaxResultVo addMeetingRecords(MeetingRecords meetingRecords) {
        try {
            if (StringUtils.isBlank(meetingRecords.getTitle())) {
                return AjaxResultVo.error("会议纪要标题不能为空！");
            }
            if (StringUtils.isBlank(meetingRecords.getContent())) {
                return AjaxResultVo.error("会议纪要内容不能为空！");
            }
            Date nowDate = new Date();
            meetingRecords.setMeetingId(Long.valueOf(UUIDUtil.getUniqueIdByUUId()));
            meetingRecords.setCreateTime(nowDate);
            meetingRecords.setUpdateTime(nowDate);
            if (meetingRecordsService.save(meetingRecords)) {
                return AjaxResultVo.success("添加会议纪要成功");
            } else {
                return AjaxResultVo.error("添加会议纪要失败");
            }
        } catch (Exception e) {
            logger.error(String.format("会议纪要添加失败 MeetingRecordsController.addMeetingRecords%s", e));
            return AjaxResultVo.error("添加会议纪要失败");
        }
    }

    /**
     * 编辑会议纪要详情
     *
     * @param model 页面跳转
     * @param meetingId 会议纪要ID
     * @return 跳转会议纪要详情页面
     */
    @RequiresPermissions("meetingRecords:edit")
    @GetMapping("/edit")
    @Operation(summary = "跳转到编辑会议纪要页面", description = "根据会议纪要ID跳转到编辑页面")
    @Parameter(name = "meetingId", description = "会议纪要ID", required = true)
    public String detail(Model model, String meetingId) {
        MeetingRecords meetingRecords = meetingRecordsService.getById(meetingId);
        model.addAttribute("meetingRecords", meetingRecords);
        return "meetingrecords/detail";
    }

    /**
     * 编辑会议纪要
     *
     * @param meetingRecords 编辑会议纪要参数
     * @return AjaxResultVo 编辑会议纪要结果
     */
    @RequiresPermissions("meetingRecords:edit")
    @PostMapping("/edit")
    @ResponseBody
    @Operation(summary = "编辑会议纪要", description = "更新会议纪要信息")
    @Parameter(name = "meetingRecords", description = "会议纪要实体", required = true)
    public AjaxResultVo editMeetingRecords(MeetingRecords meetingRecords) {
        try {
            meetingRecords.setUpdateTime(new Date());
            if (meetingRecordsService.updateById(meetingRecords)) {
                return AjaxResultVo.success("编辑会议纪要成功");
            } else {
                return AjaxResultVo.error("编辑会议纪要失败");
            }
        } catch (Exception e) {
            logger.error(String.format("会议纪要编辑失败：%s", e));
            return AjaxResultVo.error("编辑会议纪要失败");
        }
    }

    /**
     * 删除会议纪要
     *
     * @param meetingId 会议纪要ID
     * @return AjaxResultVo 删除会议纪要结果
     */
    @RequiresPermissions("meetingRecords:delete")
    @PostMapping("/delete")
    @ResponseBody
    @Operation(summary = "删除会议纪要", description = "根据会议纪要ID删除会议纪要记录")
    @Parameter(name = "meetingId", description = "会议纪要ID", required = true)
    public AjaxResultVo deleteMeetingRecords(String meetingId) {
        try {
            if (StringUtils.isEmpty(meetingId)) {
                return AjaxResultVo.error("删除失败,未选择删除选项");
            }
            if (meetingRecordsService.removeById(meetingId)) {
                return AjaxResultVo.success("删除成功");
            }
            return AjaxResultVo.error("删除失败");
        } catch (Exception e) {
            logger.error(String.format("会议纪要删除失败 MeetingRecordsController.deleteMeetingRecords%s", e));
            return AjaxResultVo.error("删除失败");
        }
    }
}